package controllers

import (
	"read.com/admin/models"
)

type RankController struct {
	BaseController
}



func (self *RankController) List() {
	self.Data["pageTitle"] = "排行榜配置表"
	sysRanks,_ := models.GetAllSysRnk()
	self.Data["rank"] = sysRanks
	self.display()
}

func (self *RankController) Table() {
	channel, _ := self.GetInt8("channel")
	rank, _ := self.GetInt("rank")

	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}

	rankList, total := models.GetRankList(page,limit,channel,rank)

	self.ajaxList("成功", MSG_OK, int64(total), rankList)
}
func (self *RankController) Add() {
	bookId, _ := self.GetInt("id")
	channel, _ := self.GetInt8("channel")
	rankId, _ := self.GetInt("rank")
	err := models.AddRankBook(bookId, channel, rankId)
	if err != nil {
		self.ajaxMsg("新增失败", MSG_ERR)
	} else {
		self.ajaxMsg("新增成功", MSG_OK)
	}
}

func (self *RankController) Edit() {
	bookId, _ := self.GetInt("id")
	channel, _ := self.GetInt8("channel")
	rankId, _ := self.GetInt("rankId")
	sortId, _ := self.GetInt("sortId")

	err := models.UpdateConfigRanking(bookId, channel, rankId, sortId)
	if err != nil {
		self.ajaxMsg("编辑失败", MSG_ERR)
	} else {
		self.ajaxMsg("编辑成功", MSG_OK)
	}
}

func (self *RankController) Del() {
	bookId, _ := self.GetInt("bookId")
	channel, _ := self.GetInt8("channel")
	rankId, _ := self.GetInt("rankId")
	sortId, _ := self.GetInt("sortId")

	err := models.DelConfigRanking(bookId, channel, rankId, sortId)
	if err != nil {
		self.ajaxMsg("删除失败", MSG_ERR)
	} else {
		self.ajaxMsg("删除成功", MSG_OK)
	}
}

func (self *RankController) Empty() {
	channel, _ := self.GetInt8("channel")
	rankId, _ := self.GetInt("rankId")

	err := models.EmptyConfigRanking(channel, rankId)
	if err != nil {
		self.ajaxMsg("删除失败", MSG_ERR)
	} else {
		self.ajaxMsg("删除成功", MSG_OK)
	}
}